import fs from "fs";
import path from "path";

export function registerRoutes(p: string) {
  const exist = fs.existsSync(p);
  if (!exist) return;
  const files = fs.readdirSync(p);
  for (const file of files) {
    const abs = path.resolve(p, file);
    const stat = fs.statSync(abs);
    if (stat.isFile() && abs.endsWith(".js")) {
      const m = require(abs);
      if (typeof m.default === "function") {
        m.default();
      }
    } else if (stat.isDirectory()) {
      registerRoutes(abs);
    }
  }
}
